<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/4/8
  Time: 11:10
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.2</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="stylesheet" href="<%=request.getContextPath()%>/css/font.css">
    <link rel="stylesheet" href="<%=request.getContextPath()%>/css/xadmin.css">
    <script src="<%=request.getContextPath()%>/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="<%=request.getContextPath()%>/js/xadmin.js"></script>
</head>
<body>
<div class="x-nav">
          <span class="layui-breadcrumb">
            <a href="">首页</a>
            <a href="">演示</a>
            <a>
              <cite>导航元素</cite></a>
          </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i></a>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <table class="layui-table" id="role-permission-table" lay-filter="role-permission-table-filter"></table>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-danger" lay-event="getCheckData">提交</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">返回</button>
    </div>
</script>
<script>
    layui.use(['laydate','form','table'], function(){
        var laydate = layui.laydate;
        var form = layui.form;
        let table = layui.table;
        //获取地址栏中的参数
        let roleId = getQueryVariable("roleId");
        table.render({
            elem:"#role-permission-table",
            url:"/role/queryRolePermissions?roleId="+roleId,
            method:"get",
            toolbar: '#toolbarDemo',
            cols:[[
                {type:'checkbox'}
                ,{field:'id',  title: 'ID', sort: true}
                ,{field:'permissionName',  title: '权限名称'}
                ,{field:'url', title: '权限路径'}
            ]]
        })
        //头工具栏事件
        table.on('toolbar(role-permission-table-filter)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            let ids = [];
            switch(obj.event){
                case 'getCheckData':
                    var data = checkStatus.data;
                    $.each(data,function(index,permission){
                        ids.push(permission.id)
                    })
                    console.log(ids)
                    $.ajax({
                        url:"/role/insertRolePermissions",
                        method:"post",
                        data:{
                            "roleId":roleId,
                            "ids":ids
                        },
                        //防止深度序列化
                        traditional: true,
                        success:function(){
                            history.back();
                        }
                    })
                    break;
                case "getCheckLength":
                    history.back();
                    break;
            };
        });
    });
    function getQueryVariable(variable)
    {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i=0;i<vars.length;i++) {
            var pair = vars[i].split("=");
            if(pair[0] == variable){return pair[1];}
        }
        return(false);
    }
</script>
</html>